package com.yufu.baidu.ocr;

import android.content.Intent;

import androidx.appcompat.app.AppCompatActivity;

import com.baidu.ocr.sdk.OCR;
import com.baidu.ocr.sdk.OnResultListener;
import com.baidu.ocr.sdk.exception.OCRError;
import com.baidu.ocr.sdk.model.BankCardParams;
import com.baidu.ocr.sdk.model.BankCardResult;
import com.baidu.ocr.sdk.model.IDCardParams;
import com.baidu.ocr.sdk.model.IDCardResult;
import com.getcapacitor.JSObject;
import com.getcapacitor.PluginCall;
import com.yufu.baidu.ocr.ui.camera.CameraActivity;

import org.json.JSONException;

import java.io.File;

public class BankCard {
  private final AppCompatActivity activity;
  private final PluginCall pluginCall;

  public BankCard(AppCompatActivity activity, PluginCall call) {
    this.activity = activity;
    this.pluginCall = call;
  }

  public Intent getIntent() {
    Intent intent = new Intent(activity, CameraActivity.class);
    intent.putExtra(CameraActivity.KEY_OUTPUT_FILE_PATH, FileUtil.getSaveFile(activity.getApplication()).getAbsolutePath());
    intent.putExtra(CameraActivity.KEY_CONTENT_TYPE, CameraActivity.CONTENT_TYPE_BANK_CARD);
    return intent;
  }


  public void recBankCard(String filePath) {
    BankCardParams param = new BankCardParams();
    param.setImageFile(new File(filePath));

    OCR.getInstance(activity).recognizeBankCard(param, new OnResultListener<BankCardResult>() {
      @Override
      public void onResult(BankCardResult result) {
        if (result != null) {
          BankCardResultJSObject data = new BankCardResultJSObject(result);
          pluginCall.resolve(data.getJSObject());
        } else {
          pluginCall.reject("识别数据为空");
        }
      }

      @Override
      public void onError(OCRError error) {
        pluginCall.reject(error.getMessage());
      }
    });
  }
}
